package homework.workTest.sort;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import static java.util.Arrays.asList;
import static java.util.Arrays.sort;

/**
 * sortChange
 *
 * @author byc
 * @date 2021-11-24
 */
public class sortChange {

    public static Integer abc(Integer max, Integer min) {
        Double mamD = new BigDecimal((float) (min + 1 + (max - min) / 2)).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        Integer mid = (int) Math.ceil(mamD);
        return mid;
    }

    public static void main(String[] args) {
        List<Integer> sortList1 = asList(1, 2, 3, 5, 6, 7, 8, 9, 13, 15, 17, 18, 20);

        List<Integer> sortList = new ArrayList<>();
        sortList.addAll(sortList1);

        Integer index = 5;

//        for (int i = index; abc(sortList.get(i), sortList.get(i - 1)) == sortList.get(i); index++) {
//            Integer temp = abc(sortList.get(i), sortList.get(i - 1));
//            System.out.println(temp);
//            System.out.println(i);
//            System.out.println("-------------------");
//        }

        ObjSort objSort = new ObjSort();
        objSort.setList((ArrayList<Integer>) sortList);
        objSort.setIndex(index);
        ObjSort chanObj = (ObjSort) chan(objSort);
        ArrayList<Integer> resList = chanObj.getList();
        Integer tempIndex = chanObj.getIndex();

        resList.remove(11);
        resList.add(tempIndex, abc(resList.get(tempIndex), resList.get(tempIndex - 1)));


        System.out.println(sortList1);
        System.out.println(resList);

        System.out.println(resList.removeAll(sortList));

    }

    public static Object chan(ObjSort objSort) {
        ArrayList<Integer> list = objSort.getList();
        Integer index = objSort.getIndex();

        Integer sort = list.get(index);
        Integer bigSort = abc(list.get(index), list.get(index - 1));

        if (sort.equals(bigSort)) {
            index = index + 1;
            objSort.setIndex(index);
            return chan(objSort);
        } else {
            objSort.setSort(bigSort);
            return objSort;
        }
    }

}

